﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using Water.Web;
using Water.Web.Controls.BaseInterface;

[assembly: WebResource("Water.Resources.Default.JS.Base.ui.core.js", "text/javascript")]
[assembly: WebResource("Water.Resources.Default.JS.Datepicker.ui.datepicker.js", "text/javascript")]
[assembly: WebResource("Water.Resources.Default.JS.Base.ui.i18n.ui.datepicker-zh-CN.js", "text/javascript")]
[assembly: WebResource("Water.Resources.Default.JS.Datepicker.ui.datepicker.help.js", "text/javascript")]
namespace Water.Web.Controls
{
    [ToolboxData("<{0}:Datepicker runat=server></{0}:Datepicker>")]
    public partial class Datepicker : BaseTextBox
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.TokenCssClass = "field_token_datepicker";
        }
        protected override void OnLoad(EventArgs e)
        {
            string js;
            if (this.ht.Count > 0)
                js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker(" + toKeyValue() + ");})\n";
            else
                js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker();})\n";
            if(!string.IsNullOrEmpty(js))
               this.UpdateJS = js;
            base.OnLoad(e);
            this.Page.ClientScript.RegisterClientScriptResource(typeof(PageManager), "Water.Resources.Default.JS.Base.ui.core.js");
            this.Page.ClientScript.RegisterClientScriptResource(typeof(Datepicker), "Water.Resources.Default.JS.Datepicker.ui.datepicker.js");
            this.Page.ClientScript.RegisterClientScriptResource(typeof(Datepicker), "Water.Resources.Default.JS.Base.ui.i18n.ui.datepicker-zh-CN.js");
            this.Page.ClientScript.RegisterClientScriptResource(typeof(Datepicker), "Water.Resources.Default.JS.Datepicker.ui.datepicker.help.js");
            PageManager.RegisterPageStyleSheet(this, "ui.all.css", "CSS/jquery.ui/ui.all.css");
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (!string.IsNullOrEmpty(this.UpdateJS))
            {
                string js = UpdateJS;
                //if(this.ht.Count>0)
                //    js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker(" + toKeyValue() + ");})\n";
                //else
                //    js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker();})\n";
                this.Page.ClientScript.RegisterStartupScript(typeof(Datepicker), this.UniqueID, js, true);
            }
            
        }
        protected override void RenderTokenAttribute(HtmlTextWriter writer)
        {
            //string js = @"$j(document).ready(function(){$j('#" + this.ClientID + "').focus();})";
            //string js;
            //if (this.ht.Count > 0)
            //    js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker(" + toKeyValue() + ");})\n";
            //else
            //    js = "$j(document).ready(function(){$j('#" + this.ClientID + "').datepicker();})\n";

            string js = @"$j('#" + this.ClientID + "').focus();";
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, js);
        }
        /*
       showOn: 'focus', // 'focus' for popup on focus,
			// 'button' for trigger button, or 'both' for either
		showAnim: 'show', // Name of jQuery animation for popup
		showOptions: {}, // Options for enhanced animations
		defaultDate: null, // Used when field is blank: actual date,
			// +/-number for offset from today, null for today
		appendText: '', // Display text following the input box, e.g. showing the format
		buttonText: '...', // Text for trigger button
		buttonImage: '', // URL for trigger button image
		buttonImageOnly: false, // True if the image appears alone, false if it appears on a button
		hideIfNoPrevNext: false, // True to hide next/previous month links
			// if not applicable, false to just disable them
		navigationAsDateFormat: false, // True if date formatting applied to prev/today/next links
		gotoCurrent: false, // True if today link goes back to current selection instead
		changeMonth: false, // True if month can be selected directly, false if only prev/next
		changeYear: false, // True if year can be selected directly, false if only prev/next
		showMonthAfterYear: false, // True if the year select precedes month, false for month then year
		yearRange: '-10:+10', // Range of years to display in drop-down,
			// either relative to current year (-nn:+nn) or absolute (nnnn:nnnn)
		showOtherMonths: false, // True to show dates in other months, false to leave blank
		calculateWeek: this.iso8601Week, // How to calculate the week of the year,
			// takes a Date and returns the number of the week for it
		shortYearCutoff: '+10', // Short year values < this are in the current century,
			// > this are in the previous century,
			// string value starting with '+' for current year + value
		minDate: null, // The earliest selectable date, or null for no limit
		maxDate: null, // The latest selectable date, or null for no limit
		duration: 'normal', // Duration of display/closure
		beforeShowDay: null, // Function that takes a date and returns an array with
			// [0] = true if selectable, false if not, [1] = custom CSS class name(s) or '',
			// [2] = cell title (optional), e.g. $.datepicker.noWeekends
		beforeShow: null, // Function that takes an input field and
			// returns a set of custom settings for the date picker
		onSelect: null, // Define a callback function when a date is selected
		onChangeMonthYear: null, // Define a callback function when the month or year is changed
		onClose: null, // Define a callback function when the datepicker is closed
		numberOfMonths: 1, // Number of months to show at a time
		showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0)
		stepMonths: 1, // Number of months to step back/forward
		stepBigMonths: 12, // Number of months to step back/forward for the big links
		altField: '', // Selector for an alternate field to store selected dates into
		altFormat: '', // The date format to use for the alternate field
		constrainInput: true, // The input is constrained by the current date format
		showButtonPanel: false // True to show button panel, false to not show it
        */
        private Hashtable ht = new Hashtable();

        private string onSelect;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("OnSelect")]
        public string OnSelect
        {
            get { return onSelect; }
            set 
            { 
                onSelect = value;
                setHashtable("onSelect", value);
            }
        }
        private string onChangeMonthYear;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("OnChangeMonthYear")]
        public string OnChangeMonthYear
        {
            get { return onChangeMonthYear; }
            set 
            { 
                onChangeMonthYear = value;
                setHashtable("onChangeMonthYear", value);
            }
        }
        private string onClose;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("OnClose")]
        public string OnClose
        {
            get { return onClose; }
            set 
            { 
                onClose = value;
                setHashtable("onClose", value);
            }
        }

        private int duration=0;
        //Behavior  Appearance
        [Browsable(true), Category("Behavior"), GlobalizeDescription("Duration"), DefaultValue(0)]
        public int Duration
        {
            get { return duration; }
            set { duration = value; setHashtable("duration", value); }
        }
        private bool changeMonth=true;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("ChangeMonth"),DefaultValue(true)]
        public bool ChangeMonth
        {
            get { return changeMonth; }
            set { changeMonth = value; setHashtable("changeMonth", value); }
        }
        private bool changeYear=true;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("ChangeYear"), DefaultValue(true)]
        public bool ChangeYear
        {
            get { return changeYear; }
            set { changeYear = value; setHashtable("changeYear", value); }
        }
        private bool showButtonPanel=true;

        [Browsable(true), Category("Appearance"), GlobalizeDescription("ShowButtonPanel"), DefaultValue(true)]
        public bool ShowButtonPanel
        {
            get { return showButtonPanel; }
            set { showButtonPanel = value; setHashtable("showButtonPanel", value); }
        }
        private bool showOtherMonths=true;

        [Browsable(true), Category("Appearance"), GlobalizeDescription("ShowOtherMonths"), DefaultValue(true)]
        public bool ShowOtherMonths
        {
            get { return showOtherMonths; }
            set { showOtherMonths = value; setHashtable("showOtherMonths", value); }
        }
        private int numberOfMonths=1;

        [Browsable(true), Category("Appearance"), GlobalizeDescription("NumberOfMonths"),DefaultValue(1)]
        public int NumberOfMonths
        {
            get { return numberOfMonths; }
            set { numberOfMonths = value; setHashtable("numberOfMonths", value); }
        }
        private int showCurrentAtPos=0;

        [Browsable(true), Category("Appearance"), GlobalizeDescription("ShowCurrentAtPos"),DefaultValue(0)]
        public int ShowCurrentAtPos
        {
            get { return showCurrentAtPos; }
            set { showCurrentAtPos = value; setHashtable("showCurrentAtPos", value); }
        }
        private string yearRange = "-10:+10";

        [Browsable(true), Category("Appearance"), GlobalizeDescription("YearRange"), DefaultValue("-10:+10")]
        public string YearRange
        {
            get { return yearRange; }
            set { yearRange = value; setHashtable("yearRange", value,true); }
        }
        private string otherSetting;

        [Browsable(true), Category("Behavior"), GlobalizeDescription("OtherSetting")]
        public string OtherSetting
        {
            get { return otherSetting; }
            set { otherSetting = value; }
        }

        


        private void setHashtable(object key, object value,bool isString)
        {
            JSON.JSONSettingUtil.SetHashtable(ht, key, value, isString);
            //if (value is string && string.IsNullOrEmpty(value+""))
            //{
            //    if (ht.ContainsKey(key))
            //        ht.Remove(key);
            //}
            //else if (value is int && value is bool)
            //{
            //    if (ht.ContainsKey(key))
            //        ht.Remove(key);
            //}
            //else
            //{
            //    if (isString)
            //       ht.Add(key, "\'"+value + "\'");
            //    else
            //        ht.Add(key, value + "");
            //}
        }
        private void setHashtable(object key, object value)
        {
            JSON.JSONSettingUtil.SetHashtable(ht, key, value);
            //setHashtable(key,value, false);
        }
        private string toKeyValue()
        {
            string strReturn="";
            //foreach (DictionaryEntry item in ht)
            //{
            //    strReturn += item.Key + ":" + item.Value.ToString().ToLower()+",";
                
            //}
            //if (strReturn.EndsWith(","))
            //    strReturn = strReturn.Substring(0, strReturn.Length - 1);

            strReturn=JSON.JSONSettingUtil.ToKeyValue(ht);

            if (!string.IsNullOrEmpty(this.OtherSetting))
                strReturn += "," + this.OtherSetting;
            return "{"+strReturn+"}";
        }
    }
}
